<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>前景</title>
        <link rel="stylesheet" href="../../assets/less/base/main.css" />
        <link rel="stylesheet" href="../../assets/less/base/iview.min.css" />
        <link rel="stylesheet" href="../../assets/less/login/index.css" />
    </head>

    <body>
        <div id="app">
            <div class="iot-login-panel">
                <div class="container-card ivu-card ivu-card-dis-hover">
                    <div class="ivu-card-body">
                        <p class="login-title">惠州·中科院智慧管理平台</p>
                        <div class="container-box">
                            <div class="ivu-login">
                                <form autocomplete="off" class="ivu-form ivu-form-label-right">
                                    <div class="ivu-login-username">
                                        <div class="ivu-form-item ivu-form-item-required">
                                            <!---->
                                            <div class="ivu-form-item-content">
                                                <div class="ivu-input-wrapper ivu-input-wrapper-large ivu-input-type-text">
                                                    <input
                                                        autocomplete="off"
                                                        spellcheck="false"
                                                        type="text"
                                                        placeholder="请输入用户名"
                                                        name="username"
                                                        v-model="userName"
                                                        class="ivu-input ivu-input-large ivu-input-with-prefix login-input"
                                                    />
                                                </div>
                                                <!---->
                                            </div>
                                        </div>
                                    </div>
                                    <div class="ivu-login-password">
                                        <div class="ivu-form-item ivu-form-item-required">
                                            <!---->
                                            <div class="ivu-form-item-content">
                                                <div class="ivu-input-wrapper ivu-input-wrapper-large ivu-input-type-password">
                                                    <input
                                                        autocomplete="off"
                                                        spellcheck="false"
                                                        type="password"
                                                        placeholder="请输入密码"
                                                        name="password"
                                                        v-model="passWord"
                                                        class="ivu-input ivu-input-large ivu-input-with-prefix login-input"
                                                    />
                                                </div>
                                                <!---->
                                            </div>
                                        </div>
                                    </div>
                                    <div class="ivu-login-submit">
                                        <button type="button" class="ivu-btn ivu-btn-primary ivu-btn-long ivu-btn-large" :disabled="loading" @click="onLogin()">
                                            <!---->
                                            <!---->
                                            <span>登录</span>
                                        </button>
                                        <p style="color: #ed4014; font-weight: 700; text-align: center; margin-top: 12px" v-if="loginMsg">{{loginMsg}}</p>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <div class="container-other"></div>
                    </div>
                </div>
            </div>
        </div>
        <script src="../../config.js"></script>
        <script src="../../assets/js/main.js" type="module"></script>
        <script src="../../assets/lib/md5.js"></script>
        <script type="module">
            // 创建Vue实例
            new Vue({
                el: '#app', // 绑定元素
                data: {
                    userName: '',
                    passWord: '',
                    loading: false,
                    loginMsg: ''
                },
                mounted() {
                    this.Init()
                },
                methods: {
                    Init() {
                        // 需要保存密码?
                        this.userName = localStorage.getItem('last_user_un')
                        this.passWord = localStorage.getItem('last_user_pw')
                    },
                    onLogin() {
                        if (this.loading) {
                            return
                        }
                        this.loading = true
                        let obj = {
                            username: this.userName,
                            password: md5(this.passWord),
                            loginType: 0,
                            clientType: 0
                        }
                        webApi.accountLoginOld(obj).then(res => {
                            if (res.code === 0) {
                                let token = res.result ? res.result.token : ''
                                Fuck(
                                    'func_db',
                                    null,
                                    'set',
                                    JSON.stringify({
                                        key: 'web_token',
                                        value: token
                                    })
                                )
                                
								let expire = res.result.expired ? res.result.expired : 0
								Fuck(
                                    'func_db',
                                    null,
                                    'set',
                                    JSON.stringify({
                                        key: 'web_token_expire',
                                        value: expire
                                    })
                                )

								
                                var result = Fuck('func_user', null, 'login_from_text', JSON.stringify(res.result))
                                if (result === 'Success') {
                                    // 获取用户信息
                                    this.getUserInfo()
                                } else {
                                    this.onLoginFail(res.msg)
                                    console.error('登录失败')
                                }
                            } else {
                                this.onLoginFail(res.msg)
                                console.error(res.msg)
                            }
                        })
                    },
                    onLoginFail(msg = '登陆失败') {
                        this.loading = false
                        this.loginMsg = msg
                    },
                    // 获取用户信息
                    getUserInfo() {
                        webApi
                            .accountInfo({})
                            .then(async res => {
                                console.log('111')
                                if (res.code !== 0) {
                                    this.onLoginFail('登录失败，获取用户权限信息失败')
                                    return
                                }
                                let result = res.result
                                let roleNames = result.role ? result.role.map(x => x.name) : []
                                var userInfo = {
                                    name: result.name,
                                    avatar: '',
                                    access: roleNames
                                }

                                Fuck(
                                    'func_db',
                                    null,
                                    'set',
                                    JSON.stringify({
                                        key: 'web_roleName',
                                        value: JSON.stringify(roleNames)
                                    })
                                )
                                Fuck(
                                    'func_db',
                                    null,
                                    'set',
                                    JSON.stringify({
                                        key: 'web_userId',
                                        value: result.id
                                    })
                                )
                                Fuck(
                                    'func_db',
                                    null,
                                    'set',
                                    JSON.stringify({
                                        key: 'web_userName',
                                        value: result.name
                                    })
                                )

                                //console.log('获取用户信息成功，当前用户角色为：' + roleNames.join(','))
                                // 获取用户所属组织信息
                                webApi
                                    .getOrgByUser(result.id, {
                                        root: true
                                    })
                                    .then(orgRes => {
                                        if (orgRes.code !== 0) {
                                            this.onLoginFail('登录失败，获取用户所属项目失败')
                                            return
                                        }

                                        let orgResult = util.getArrData(orgRes.result)
                                        if (util.isEmpty(orgResult)) {
                                            this.onLoginFail('登录失败，所属项目为空')
                                            return
                                        }
                                        // 存储用户的组织Id
                                        Fuck(
                                            'func_db',
                                            null,
                                            'set',
                                            JSON.stringify({
                                                key: 'web_projId',
                                                value: orgResult[0].id
                                            })
                                        )

                                        // 用户数据获取完毕 开始执行三维逻辑

                                        // 保存用户名和密码
                                        localStorage.setItem('last_user_un', this.userName)
                                        localStorage.setItem('last_user_pw', this.passWord)

                                        // 要保存一下密码，设置编辑时使用这个免登录
                                        Fuck(
                                            'func_db',
                                            null,
                                            'set',
                                            JSON.stringify({
                                                key: 'login_user_name',
                                                value: this.userName
                                            })
                                        )
                                        Fuck(
                                            'func_db',
                                            null,
                                            'set',
                                            JSON.stringify({
                                                key: 'login_user_pw',
                                                value: md5(this.passWord)
                                            })
                                        )

                                        Fuck('func_html_page', __self__, 'delete_page')
                                        Fuck('func_zky', __self__, 'OnLogin')

                                        Fuck('func_html_page', 'Menu', 'show')
                                        FuckPage('Menu', 'window.OnLoginSuccess')
                                    })
                            })
                            .catch(e => {
                                this.onLoginFail(res.msg)
                                console.error(e)
                            })
                    }
                }
            })
        </script>
    </body>
</html>
